#
# "@(#) $Id: SearchWidget.py,v 1.1.1.1 2006-07-27 03:16:08 gioshe Exp $"
#
# This work is released under the GNU GPL, version 2 or later.
#
from Utils.kdeemul import *
from PixmapPushButton import *
from Utils.Paths import *
from Utils.utils import *
from Utils.Settings import *


_labels = None
_timerDuration=800	
		
class SearchWidget(QWidget):
	def __init__(self,container=None):
		QWidget.__init__(self,container)
		global _labels
		if _labels==None:
			_labels = { 'all':i18n('Search'), 'artists':i18n('Search Artists'), 'albums':i18n('Search Albums'), 'composers':i18n('Search Composers'), 'titles':i18n('Search Songs'), 'genres':i18n('Search Genres')}
		self.searchType = 'all'
		self.searchText = ''
		#self.setFixedWidth(140)
		self.setFixedHeight(20)
		self.middle = QBrush()
		self.middle.setPixmap(QPixmap(art("bgsearchmiddle.png")))
		self.initActions()
		self.initMenu()
		self.initViews()
		self.inputTimer=QTimer()
		QObject.connect(self.inputTimer,SIGNAL("timeout()"),self.emitSearchText)
		self.backColor=QColor(Settings.settings().get('ColorR'),Settings.settings().get('ColorG'),Settings.settings().get('ColorB'))
		self.setPaletteBackgroundColor(self.backColor)

	def initActions(self):
		self.collection = KActionCollection(self)
		self.searchAllAction = KRadioAction(i18n("&All"),KShortcut.null(),self.slotSearchAll,self.actionCollection(),"searchAll")
		self.searchArtistsAction = KRadioAction(i18n("A&rtists"),KShortcut.null(),self.slotSearchArtists,self.actionCollection(),"searchArtists")
		self.searchAlbumsAction = KRadioAction(i18n("A&lbums"),KShortcut.null(),self.slotSearchAlbums,self.actionCollection(),"searchAlbums")
		self.searchComposersAction = KRadioAction(i18n("&Composers"),KShortcut.null(),self.slotSearchComposers,self.actionCollection(),"searchComposers")
		self.searchTitlesAction = KRadioAction(i18n("&Songs"),KShortcut.null(),self.slotSearchTitles,self.actionCollection(),"searchTitles")
		self.searchGenresAction = KRadioAction(i18n("&Genres"),KShortcut.null(),self.slotSearchGenres,self.actionCollection(),"searchGenres")
		self.searchAllAction.setExclusiveGroup("search")
		self.searchArtistsAction.setExclusiveGroup("search")
		self.searchAlbumsAction.setExclusiveGroup("search")
		self.searchComposersAction.setExclusiveGroup("search")
		self.searchTitlesAction.setExclusiveGroup("search")
		self.searchGenresAction.setExclusiveGroup("search")

		self.searchAllAction.setChecked(True)

	def actionCollection(self):
		return self.collection

	def initMenu(self):
		self.searchTypeMenu = QPopupMenu()
		self.searchTypeMenu.insertItem(i18n('Search'),0)
		self.searchTypeMenu.setItemEnabled(0,False)
		self.searchTypeMenu.insertSeparator()
		self.searchAllAction.plug(self.searchTypeMenu)
		self.searchArtistsAction.plug(self.searchTypeMenu)
		self.searchAlbumsAction.plug(self.searchTypeMenu)
		self.searchComposersAction.plug(self.searchTypeMenu)
		self.searchTitlesAction.plug(self.searchTypeMenu)
		self.searchGenresAction.plug(self.searchTypeMenu)

	def initViews(self):
		vlayout=QVBoxLayout(self)
		vlayout.setAlignment(Qt.AlignCenter)
		lSearch = QHBoxLayout(self)
		lSearch.addSpacing(3)
		vlayout.addLayout(lSearch)
		layoutSearch = QHBoxLayout(lSearch)
		layoutSearch.setMargin(4)
		self.searchTypeButton = SmallPixmapPushButton(QIconSet(QPixmap(art("buttonsearch.png"))),"",self,None,QColor(217,221,236))
		self.searchTypeButton.setFixedSize(QSize(18,18))
		QToolTip.add(self.searchTypeButton,i18n(" find songs, artists and albums"))
		QObject.connect(self.searchTypeButton,SIGNAL("clicked()"),self.slotSearchType)
		layoutSearch.addWidget(self.searchTypeButton)
		self.searchTextEdit = QLineEdit(self)
		QObject.connect(self.searchTextEdit,SIGNAL("textChanged(const QString &)"),self.setSearchText)
		QObject.connect(self.searchTextEdit,SIGNAL("returnPressed()"),self.searchReturnPressed)
		layoutSearch.addWidget(self.searchTextEdit)
		self.searchTextEdit.setFrame(False)
		self.backColor=QColor(Settings.settings().get('ColorR'),Settings.settings().get('ColorG'),Settings.settings().get('ColorB'))
		self.searchTextEdit.setPaletteBackgroundColor(self.backColor)
		#self.searchTextEdit.setPaletteBackgroundColor(QColor(217,221,236))
		self.clearBtn=SmallPixmapPushButton(QIconSet(QPixmap(art("buttonstatuscancel.png"))),"",self,None,QColor(217,221,236))
		#self.clearBtn.hide()
		layoutSearch.addWidget(self.clearBtn)
		QToolTip.add(self.clearBtn,i18n("Clear the search field"))
		QObject.connect(self.clearBtn,SIGNAL("clicked()"),self.clearText)
		lSearch.addSpacing(3)

	def setFocus(self):
		self.searchTextEdit.setFocus()

	def clearText(self):
		self.searchTextEdit.clear()
		self.hide()
		
	def setSearchType(self,value):
		global _labels
		self.searchType = value
		#print "setting searchType to",value
		if self.searchText!="":
			self.emit(PYSIGNAL("setSearchType"),(self.searchType,None))
		self.searchLabel.setText(_labels[value])

	def slotSearchAll(self):
		self.setSearchType('all')

	def slotSearchArtists(self):
		self.setSearchType('artists')

	def slotSearchAlbums(self):
		self.setSearchType('albums')

	def slotSearchComposers(self):
		self.setSearchType('composers')

	def slotSearchTitles(self):
		self.setSearchType('titles')
	
	def slotSearchGenres(self):
		self.setSearchType('genres')

	def slotSearchType(self):
		self.searchTypeMenu.exec_loop(QCursor.pos())

	def setSearchText(self,text):
		if self.inputTimer.isActive():
			self.inputTimer.stop()
		
		newText = unikode(text).strip()
		#if newText!="" and not self.clearBtn.isVisible():
		#	#print "showing clear btn"
		#	self.clearBtn.show()
		#elif newText=="":
		#	self.clearBtn.hide()
		if newText!=self.searchText:
			self.searchText = newText
		print "match text is ",self.searchText
		self.inputTimer.start(_timerDuration)
		
	def emitSearchText(self):
		print "emitting search text", self.searchText
		self.emit(PYSIGNAL("setSearchText"),(self.searchText,None))
		self.inputTimer.stop()

	def searchReturnPressed(self):
		if len(self.searchText)>0:
			self.emit(PYSIGNAL("searchWithTextAndType"),(self.searchText,self.searchType))
